var modalController = new Class({
	Implements: Options,
	options: {
		customClass: '',
		customTitle: '',
		customURI: ''
	},
	loadContent: function(){},
	initModal: function(){},
	box: null,
	container: null,
	initialize: function(options){
		if(typeOf(options) == 'object')
			this.setOptions(options);
		if(typeOf(openSubpage) == 'object' && 'dispose' in openSubpage) openSubpage.dispose(true);
		openSubpage = this;
		if(typeOf(cal) == 'object') this.calCtrl = cal;

		this.container = $(document.body);
		this.initModal.apply(this, this.initialize.arguments);
		if('loadData' in this)
			this.loadData();
		else
			this.loadPage();
	},
	loadPage: function(){
		var self = this;
		$('wrapper').hide();
		document.getElement('html').addClass('nofollow');
		
		 
		this.box = new Element('div.seriaBox').fade('hide');
		if(!this.lightsOn()) this.box.addClass('black');
		if(this.options.customClass.length) this.box.addClass(this.options.customClass);
		
		this.page = new Element('div.seriaPage').inject(this.box);
		
		var logo = new Element('div.logo', {html: 'Mit n&eacute;zel?'}).inject(this.page);
		this.lightSwitchElement = new Element('a.ir[title=Villanykapcsoló (L)][accesskey=L]', {'html' : 'Villanykapcsoló (L)'})
		.inject(logo)
		.addEvent('click', function(e){
			e.preventDefault();
			self.lightSwitch();
		});
		if(typeof k == 'object') k.addNew(this.lightSwitchElement);
		if(this.lightsOn()) this.lightSwitchElement.addClass('on');
			
		this.wrap = new Element('div.wrap').inject(this.page);
		
		this.loadContent.apply(this, this.loadPage.arguments);
		this.setAddressBar();
		this.box.inject(this.container, 'bottom');
		this.box.fade('in');
		
	},
	setAddressBar: function(clean){
		if(clean)
		{
			this.hash = '';
			if('pushState' in window.history)
			{
				window.history.pushState({param: 0, value: 0 }, title.retrieve('title'), '/');
			}
			else {
				new URI().set('fragment', 'top').go();
			}
			document.title = title.retrieve('title');
			return;
		}
		this.hash = '/' + this.options.customURI;
		if('pushState' in window.history)
		{
			state = {param: this.options.customURI, value: null };
			
			if(Object.toQueryString(currentState) != Object.toQueryString(state))
				window.history.pushState(state, title.retrieve('title') + ' ~ ' + this.options.customTitle, this.hash);
			else
				window.history.replaceState(state, title.retrieve('title') + ' ~ ' + this.options.customTitle, this.hash);
		}
		else {
			new URI().set('fragment', this.hash).go();
		}
		document.title = title.retrieve('title') + ' ~ ' + this.options.customTitle;
	},
	dispose: function(withoutAnimation){	
		var dispose = function(){
			if(this.box) this.box.dispose();
			this.setAddressBar(true);
			document.getElement('html').removeClass('nofollow');

			openSubpage = {};
		}.bind(this);
		$('wrapper').show();
		if(withoutAnimation)
			dispose();
		else if(this.box)
			new Fx.Tween(this.box, {property: 'opacity'}).start(1, 0).chain(dispose);
		else
			dispose();
	},
	lightsOn: function(){
		return Boolean((Cookie.read('sc_lightsOn') || 0).toInt());
	},
	lightSwitch: function(){
		this.lightSwitchElement.toggleClass('on');
		this.box.toggleClass('black');
		Cookie.write('sc_lightsOn', ((this.box.hasClass('black')) ? 0 : 1), {duration: 14, path: '/'});
	}
});